package zhoufu.sz.sanxiao_check;

import android.databinding.ViewDataBinding;
import android.text.TextUtils;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

import javax.inject.Inject;

import zhoufu.sz.sanxiao_check.databinding.ActivityAddFiveInOneBinding;
import zhoufu.sz.sanxiao_check.inject.components.DaggerLoginComponet;
import zhoufu.sz.sanxiao_check.models.AddFiveInOneModel;
import zhoufu.sz.sanxiao_check.models.BaseModel;
import zhoufu.sz.sanxiao_check.presents.AddFiveInOnePresenter;
import zhoufu.sz.sanxiao_check.ui.basic.BaseMsgActivity;
import zhoufu.sz.sanxiao_check.ui.contract.AddFiveInOneContract;

/**
 * 添加5位一体信息
 */
public class AddFiveInOneActivity extends BaseMsgActivity implements AddFiveInOneContract.View {

    ActivityAddFiveInOneBinding binding;

    @Inject
    AddFiveInOnePresenter presenter;

    @Override
    protected void initInject() {
        DaggerLoginComponet.builder()
                .activityModule(getActivityModule())
                .appComponent(getAppComponent())
                .build().inject(this);
    }

    @Override
    protected int setIndividualId() {
        return R.layout.activity_add_five_in_one;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        menu.add("保存").setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
//            @Override
//            public boolean onMenuItemClick(MenuItem item) {
//                //保存
//                save();
//                return true;
//            }
//        });
        return super.onCreateOptionsMenu(menu);
    }

    private void save() {
        AddFiveInOneModel model = new AddFiveInOneModel();
        model.SID = mInfoModel.getSID();
        model.Community = getTextDefault(binding.etSheZerenName);
        model.CommunityArea = getTextDefault(binding.etShePianName);
        model.Department1 = getTextDefault(binding.etStreetDepartment1);
        model.Department2 = getTextDefault(binding.etStreetDepartment2);
        model.Department3 = getTextDefault(binding.etStreetDepartment3);
        model.Department4 = getTextDefault(binding.etStreetDepartment4);
        model.Department5 = getTextDefault(binding.etStreetDepartment5);
        model.Department6 = getTextDefault(binding.etStreetDepartment6);
        model.District = getTextDefault(binding.etDepartmentManage3);
        model.DistrictPhone = getTextDefault(binding.etDepartmentManagePhone3);
        model.Enforce = getTextDefault(binding.etDepartmentCharge3);
        model.EnforceArea = getTextDefault(binding.etDepartmentPianCharge3);
        model.Fire = getTextDefault(binding.etDepartmentManage2);
        model.FirePhone = getTextDefault(binding.etDepartmentManagePhone2);
        model.Leadership = getTextDefault(binding.etStreetLeaderName);
        model.Leader = getTextDefault(binding.etStreetSheName);
        model.Police = getTextDefault(binding.etDepartmentCharge1);
        model.PoliceArea = getTextDefault(binding.etDepartmentPianCharge1);
        model.Special = getTextDefault(binding.etSheSafeGenerate);
        model.Supervise = getTextDefault(binding.etDepartmentCharge2);
        model.SuperviseArea = getTextDefault(binding.etDepartmentPianCharge2);
        presenter.update(model);
    }

    @Override
    protected void initAfterOnCreate(ViewDataBinding binding) {
        super.initAfterOnCreate(binding);
        this.binding = (ActivityAddFiveInOneBinding) binding;
        presenter.bindView(this);
        presenter.loadData(mInfoModel.getSID());
        setTitle("五位一体信息");
    }


    String getTextDefault(TextView textView) {
        String text = textView.getText().toString();
        return TextUtils.isEmpty(text) ? "无" : text;
    }

    @Override
    public void onLoadSuccess(AddFiveInOneModel model) {
        binding.setVariable(BR.model,model);
        binding.executePendingBindings();
        binding.etDepartmentManage1.setText(mInfoModel.getSafety_Name());
        binding.etDepartmentManagePhone1.setText(mInfoModel.getSafety_Phone());
    }

    @Override
    public void updateSuccese(BaseModel baseModel) {
        Toast.makeText(getApplicationContext(),"保存成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void pullFaild() {
        Toast.makeText(getApplicationContext(), "网络异常", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void pullEnd() {

    }

    @Override
    public void pullStart() {

    }
}
